package com.wejoy.video.middle.station.data.util;

import com.wejoy.video.middle.station.data.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.util.Map;

/**
 * 文件工具类
 * @author admin
 */
public class FileUtils {

    /**
     * 获得文件的扩展名
     * @param fileName
     * @return
     */
    public static String getSuffix(String fileName) {
        if (StringUtils.isEmpty(fileName)) {
            return "";
        }
        return fileName.substring(fileName.lastIndexOf("."));
    }

    /**
     * 获得路径中包含后缀的文件名
     * @param path  文件路径
     * @return
     */
    public static String getFileName(String path) {
        return path.substring(path.lastIndexOf("/"));
    }

    public static String getFileNameWithoutSuffix(String path) {
        if (StringUtils.isEmpty(path)) {
            throw new BusinessException("文件路径非法");
        }
        // 有些人上传文件没有后缀
        if (!path.contains(".")) {
            return path;
        }
        return path.substring(0, path.lastIndexOf("."));
    }

    /**
     * 获得远程路径文件的字节码
     * @param url
     * @return
     * @throws IOException
     */
    public static byte[] getUrlBytes(String url) throws IOException {
        WJAssert.hasLength(url, "URL不合法");
        WJAssert.isTrue(url.contains("http"), "URL没有协议");
        URL requestUrl = new URL(url);
        InputStream inputStream = requestUrl.openStream();
        ReadableByteChannel src = Channels.newChannel(inputStream);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        WritableByteChannel dest = Channels.newChannel(byteArrayOutputStream);
        ByteBuffer buffer = ByteBuffer.allocate(10240);
        while (src.read(buffer) != -1) {
            buffer.flip();
            while (buffer.hasRemaining()) {
                dest.write(buffer);
            }
            buffer.clear();
        }
        inputStream.close();
        src.close();
        dest.close();
        byteArrayOutputStream.close();
        return byteArrayOutputStream.toByteArray();
    }

    /**
     * 获得远程路径的字节码
     * @param url
     * @param header
     * @return
     * @throws IOException
     */
    public static byte[] getUrlBytes(String url, Map<String, String> header) throws IOException {
        URLConnection requestUrl = new URL(url).openConnection();
        if(!CollectionUtils.isEmpty(header)) {
            header.forEach((k, v) -> requestUrl.setRequestProperty(k, v));
        }
        InputStream inputStream = requestUrl.getInputStream();
        ReadableByteChannel src = Channels.newChannel(inputStream);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        WritableByteChannel dest = Channels.newChannel(byteArrayOutputStream);
        ByteBuffer buffer = ByteBuffer.allocate(10240);
        while (src.read(buffer) != -1) {
            buffer.flip();
            while (buffer.hasRemaining()) {
                dest.write(buffer);
            }
            buffer.clear();
        }
        inputStream.close();
        src.close();
        dest.close();
        byteArrayOutputStream.close();
        return byteArrayOutputStream.toByteArray();
    }
}
